package baidu.reader.txt.ui
{
    import baidu.reader.common.*;
    import baidu.reader.common.effects.*;
    import baidu.reader.txt.model.*;
    import baidu.ui.constants.*;
    import baidu.ui.containers.*;
    import baidu.ui.layout.*;
    import baidu.ui.managers.*;
    
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    
    import mx.events.CalendarLayoutChangeEvent;

    public class TxtDoubleBookPage extends TxtBookPageBase
    {
		[Embed(source="assets/sprite39.swf")]
		private var LeftCursor:Class;
		
		[Embed(source="assets/sprite56.swf")]
		private var RightCursor:Class;
		
		[Embed(source="assets/sprite27.swf")]
		private var ClickArea:Class;
		
		[Embed(source="assets/sprite62.swf")]
		private var LeftPageBG:Class;
        private var leftBG:Sprite;
		
		
        private var leftPageContent:ScrollPane;
        private var leftTextContainer:Sprite;
        private var leftTextField:TextField;
		
		[Embed(source="assets/sprite59.swf")]
		private var RightPageBG:Class;
        private var rightBG:Sprite;
        private var rightPageContent:ScrollPane;
        private var rightTextContainer:Sprite;
        private var rightTextField:TextField;
        private var textFormat:TextFormat;
        private var screenFirstCharIndex:int;
        private var previousScrollTopPosition:Number = 0;
        private var previousScrollBottomPosition:Number = 0;
        private var _updatePending:Boolean = false;
        private var _needUpdate:Boolean = false;
        private var previousLeftVisible:Boolean = true;
        private var previousRightVisible:Boolean = true;
        private var previousLeftScrollPosition:Number;

        public function TxtDoubleBookPage()
        {
            this.leftTextContainer = new Sprite();
            this.leftTextField = new TextField();
            this.rightTextContainer = new Sprite();
            this.rightTextField = new TextField();
            return;
        }

        override protected function addPageBG() : void
        {
            this.leftBG = new LeftPageBG();
            addChild(this.leftBG);
            var _loc_1:* = this._childsLayoutInfo[this.leftBG];
            _loc_1.offsetCenterH = 0;
            _loc_1.widthPercent = 50;
            _loc_1.heightPercent = 100;
            this.rightBG = new RightPageBG();
            addChild(this.rightBG);
            var _loc_2:* = this._childsLayoutInfo[this.rightBG];
            _loc_2.offsetCenterH = 0;
            _loc_2.widthPercent = 50;
            _loc_2.heightPercent = 100;
            return;
        }

        override protected function addLeftClickArea() : void
        {
            if (!leftClickPane)
            {
                leftClickPane = new ClickArea();
                leftClickPane.alpha = 0;
            }
            addChild(leftClickPane);
            var _loc_1:* = _childsLayoutInfo[leftClickPane];
            _loc_1.left = 0;
            _loc_1.top = 20;
            _loc_1.bottom = 70;
            _loc_1.widthPercent = 25;
            CursorManager.getInstance().register(leftClickPane, LeftCursor);
            leftClickPane.addEventListener(MouseEvent.CLICK, this.previousClickHandler);
            return;
        }

        override protected function addRightClickArea() : void
        {
            if (!rightClickPane)
            {
                rightClickPane = new ClickArea();
                rightClickPane.alpha = 0;
            }
            addChild(rightClickPane);
            var _loc_1:* = _childsLayoutInfo[rightClickPane];
            _loc_1.top = 20;
            _loc_1.bottom = 70;
            _loc_1.offsetCenterH = 0;
            _loc_1.right = 55;
            CursorManager.getInstance().register(rightClickPane, RightCursor);
            rightClickPane.addEventListener(MouseEvent.CLICK, this.nextClickHandler);
            return;
        }

        private function nextClickHandler(event:MouseEvent) : void
        {
            dispatchEvent(new ReaderEvent(ReaderEvent.GO_NEXT_PAGE));
            return;
        }

        private function previousClickHandler(event:MouseEvent) : void
        {
            dispatchEvent(new ReaderEvent(ReaderEvent.GO_PREV_PAGE));
            return;
        }

        override protected function addPageContent() : void
        {
            var destScrollPosition:Number;
            if (this.updatePending)
            {
                this.needUpdate = true;
                return;
            }
            this.needUpdate = false;
            this.initTextfield(this.leftTextField);
            this.initTextfield(this.rightTextField);
            switch(chapterModel.textStrategy)
            {
                case TxtChapterModel.BOTTOM:
                {
                    this.leftTextField.appendText(chapterModel.currentChapterText);
                    this.rightTextField.appendText(chapterModel.currentChapterText);
                    destScrollPosition = this.previousScrollTopPosition;
                    break;
                }
                case TxtChapterModel.TOP:
                {
                    this.leftTextField.text = chapterModel.currentChapterText + this.leftTextField.text;
                    this.rightTextField.text = chapterModel.currentChapterText + this.rightTextField.text;
                    break;
                }
                case TxtChapterModel.REPLACE:
                {
                    this.leftTextField.text = chapterModel.currentChapterText;
                    this.rightTextField.text = chapterModel.currentChapterText;
                    destScrollPosition = 0;
                    break;
                }
                default:
                {
                    this.leftTextField.text = chapterModel.currentChapterText;
                    this.rightTextField.text = chapterModel.currentChapterText;
                    destScrollPosition =0;
                    break;
                }
            }
            this.arrayFunc([this.leftTextContainer, this.rightTextContainer], function (param1:Sprite) : void
            {
                while (param1.numChildren)
				{
                    
                    param1.removeChildAt(0);
                }
                return;
            }
            );
            this.leftTextContainer.addChild(this.leftTextField);
            this.rightTextContainer.addChild(this.rightTextField);
            this.arrayFunc([this.leftPageContent, this.rightPageContent], function (param1:ScrollPane) : void
            {
                if (param1 && param1.parent)
                {
                    param1.parent.removeChild(param1);
                }
                return;
            }
            );
            this.leftPageContent = new ScrollPane();
            this.rightPageContent = new ScrollPane();
            this.arrayFunc([this.leftPageContent, this.rightPageContent], function (param1:ScrollPane) : void
            {
                param1.cacheAsBitmap = true;
                param1.vScrollBar.setStyle("skin", ResourceFactory.AppScrollBar_Skin);
                param1.hScrollBar.setStyle("skin", ResourceFactory.AppScrollBar_Skin);
                param1.vScrollPolicy = ScrollPolicy.OFF;
                param1.hScrollPolicy = ScrollPolicy.OFF;
                addChild(param1);
                return;
            }
            );
            this.leftPageContent.addEventListener(Event.CHANGE, this.scrollChangeHandler);
            this.layoutPageContent();
            this.leftPageContent.content = this.leftTextContainer;
            this.rightPageContent.content = this.rightTextContainer;
            if (chapterModel.textStrategy == TxtChapterModel.TOP)
            {
                destScrollPosition = this.leftPageContent.content.height - this.previousScrollBottomPosition;
            }
            if (!isNaN(destScrollPosition))
            {
                this.leftPageContent.vScrollPosition = destScrollPosition;
                this.updateRightTextScroll();
            }
            this.resizePane();
            this.restorePosition();
            addClickArea();
			
            return;
        }

        private function scrollChangeHandler(event:Event = null) : void
        {
            this.previousScrollBottomPosition = this.leftPageContent.contentHeight - this.leftPageContent.vScrollPosition;
            this.previousScrollTopPosition = this.leftPageContent.vScrollPosition;
            if (this.leftPageContent.vScrollPosition / this.leftPageContent.maxVScrollPosition < 0.25)
            {
                dispatchEvent(new ReaderEvent(ReaderEvent.SCROLLED_TOP));
            }
            else if (this.scrollPercent >= 100)
            {
                dispatchEvent(new ReaderEvent(ReaderEvent.SCROLLED_BOTTOM));
            }
            dispatchEvent(new ReaderEvent(ReaderEvent.SCROLL, this.scrollPercent));
            return;
        }

        private function updateRightTextScroll() : void
        {
            var _loc_1:Number = NaN;
            if (this.leftPageContent.vScrollPosition < this.leftPageContent.maxVScrollPosition)
            {
                this.rightPageContent.visible = true;
                _loc_1 = this.leftPageContent.vScrollPosition + this.leftPageContent.vPageScrollSize;
                if (_loc_1 > this.rightPageContent.maxVScrollPosition)
                {
                    _loc_1 = this.rightPageContent.maxVScrollPosition;
                    this.leftPageContent.vScrollPosition = _loc_1 - this.rightPageContent.vPageScrollSize;
                }
                this.rightPageContent.vScrollPosition = _loc_1;
            }
            else
            {
                this.rightPageContent.visible = false;
            }
            return;
        }

        public function getCurrentLastCharIndex() : int
        {
            var _loc_1:TextField = null;
            if (this.rightTextField.text.length > 0)
            {
                _loc_1 = this.rightTextField;
            }
            else
            {
                _loc_1 = this.leftTextField;
            }
            var _loc_2:* = _loc_1.getLineIndexOfChar(_loc_1.getCharIndexAtPoint(2, _loc_1.height - 2));
            var _loc_3:* = _loc_1.getLineOffset(_loc_2) + _loc_1.getLineLength(_loc_2) + 1;
            return _loc_3;
        }

        override public function getCurrentFirstCharIndex() : int
        {
            if (!this.leftPageContent)
            {
                return 0;
            }
            var _loc_1:* = this.leftPageContent.vScrollPosition + 4;
            while (this.leftTextField.getCharIndexAtPoint(2, _loc_1) == -1 && _loc_1 < this.leftPageContent.availableHeight)
            {
                
                _loc_1 = _loc_1 + 2;
            }
            return this.leftTextField.getCharIndexAtPoint(2, _loc_1);
        }

        override public function getSecondLineFirstCharIndex() : int
        {
            if (!this.leftPageContent)
            {
                return 0;
            }
            var _loc_1:* = this.leftPageContent.vScrollPosition + this.leftTextField.getLineMetrics(0).height + 2;
            while (this.leftTextField.getCharIndexAtPoint(2, _loc_1) == -1 && _loc_1 < this.leftPageContent.availableHeight + this.leftPageContent.vScrollPosition)
            {
                
                _loc_1 = _loc_1 + 2;
            }
            var _loc_2:* = this.leftTextField.getCharIndexAtPoint(2, _loc_1);
            return _loc_2;
        }

        private function initTextfield(param1:TextField) : void
        {
            this.textFormat = new TextFormat(preferenceModel.fontName, preferenceModel.fontSize);
            this.textFormat.leading = preferenceModel.leading;
            param1.wordWrap = true;
            param1.defaultTextFormat = this.textFormat;
            param1.setTextFormat(this.textFormat, -1, (param1.text.length - 1));
            param1.width = screenModel.stageWidth / 2 - 100;
            param1.height = screenModel.stageHeight - 110;
            param1.selectable = false;
            param1.autoSize = TextFieldAutoSize.LEFT;
            param1.mouseWheelEnabled = false;
            param1.cacheAsBitmap = true;
            return;
        }

        override protected function screenResizeHandler(event:Event) : void
        {
            this.screenFirstCharIndex = this.getCurrentFirstCharIndex();
            this.initTextfield(this.leftTextField);
            this.initTextfield(this.rightTextField);
            this.layoutPageContent();
            this.resizePane();
            this.scrollToCharIndex(this.screenFirstCharIndex);
            return;
        }

        private function scrollToCharIndex(param1:int) : void
        {
            var destY1:Number;
            var charIndex:* = param1;
            if (chapterModel.useLastPage)
            {
                this.rightPageContent.vScrollPosition = this.rightPageContent.maxVScrollPosition;
                this.leftPageContent.vScrollPosition = this.rightPageContent.vScrollPosition - this.rightPageContent.vPageScrollSize;
                return;
            }
            if (charIndex < 0 || charIndex >= this.leftTextField.text.length)
            {
                return;
            }
            try
            {
                destY1 = this.leftTextField.getLineMetrics(0).height * this.leftTextField.getLineIndexOfChar(charIndex);
                this.leftPageContent.vScrollPosition = destY1 > this.leftPageContent.contentHeight ? (this.leftPageContent.contentHeight) : (destY1);
                this.updateRightTextScroll();
            }
            catch (e:Error)
            {
                trace(e.getStackTrace());
            }
            return;
        }

        private function resizePane() : void
        {
            if (!this.leftPageContent)
            {
                return;
            }
            var _loc_1:* = this.leftTextField.getLineMetrics(0).height;
            var _loc_2:* = Math.round(this.leftPageContent.height / _loc_1);
            var _loc_3:* = _loc_2 * _loc_1;
            if (_loc_3 > this.height - 60)
            {
                _loc_3 = _loc_3 - _loc_1;
            }
            this.leftPageContent.height = _loc_3;
            this.rightPageContent.height = _loc_3;
            nextChapterButton.height = _loc_1;
            nextChapterButton.drawNow();
            nextChapterButton.visible = false;
            trace("nextChapterButton.height:" + nextChapterButton.height);
            var _loc_4:* = (int((this.leftTextField.height + nextChapterButton.height) / (_loc_3 * 2)) + 1) * _loc_3 * 2 - this.leftTextField.height - nextChapterButton.height;
            if ((int((this.leftTextField.height + nextChapterButton.height) / (_loc_3 * 2)) + 1) * _loc_3 * 2 - this.leftTextField.height - nextChapterButton.height >= _loc_3)
            {
                _loc_4 = _loc_4 - _loc_3;
                this.leftTextContainer.addChild(nextChapterButton);
                nextChapterButton.drawNow();
                nextChapterButton.y = _loc_4 + this.leftTextField.height;
                nextChapterButton.x = (this.leftPageContent.availableWidth - nextChapterButton.width) / 2;
            }
            else
            {
                this.rightTextContainer.addChild(nextChapterButton);
                nextChapterButton.drawNow();
                nextChapterButton.y = _loc_4 + this.rightTextField.height;
                nextChapterButton.x = (this.rightPageContent.availableWidth - nextChapterButton.width) / 2;
            }
            this.leftPageContent.update();
            this.rightPageContent.update();
            this.updateRightTextScroll();
            return;
        }

        private function layoutPageContent() : void
        {
            if (!this.leftPageContent)
            {
                return;
            }
            this.leftPageContent.y = 45;
            this.leftPageContent.x = 40;
            this.leftPageContent.width = (screenModel.stageWidth - 200) / 2;
            this.leftPageContent.height = screenModel.stageHeight - 135;
            this.rightPageContent.y = 45;
            this.rightPageContent.x = screenModel.stageWidth / 2 + 20;
            this.rightPageContent.width = (screenModel.stageWidth - 200) / 2;
            this.rightPageContent.height = screenModel.stageHeight - 135;
            this.leftPageContent.drawNow();
            this.rightPageContent.drawNow();
            return;
        }

        override public function goNextPage() : void
        {
            var _loc_1:Number = NaN;
            if (this.leftPageContent.vScrollPosition > this.leftPageContent.maxVScrollPosition - this.leftPageContent.vPageScrollSize)
            {
                _loc_1 = 0;
            }
            else
            {
                _loc_1 = this.leftPageContent.vPageScrollSize;
            }
            this.previousLeftScrollPosition = this.leftPageContent.vScrollPosition;
            if (_loc_1 > 0)
            {
                this.leftPageContent.vScrollPosition = this.leftPageContent.vScrollPosition + _loc_1 * 2;
                this.updateRightTextScroll();
            }
            else
            {
                this.hideContent();
            }
            this.doDisplayNextPage();
            if (_loc_1 > 0)
            {
                this.leftPageContent.vScrollPosition = this.leftPageContent.vScrollPosition - _loc_1 * 2;
                this.updateRightTextScroll();
            }
            else
            {
                this.showContent();
            }
            this.updatePending = true;
            return;
        }

        private function hideContent() : void
        {
            this.previousLeftVisible = this.leftPageContent.visible;
            this.previousRightVisible = this.rightPageContent.visible;
            this.leftPageContent.visible = false;
            this.rightPageContent.visible = false;
            return;
        }

        private function showContent() : void
        {
            this.leftPageContent.visible = this.previousLeftVisible;
            this.rightPageContent.visible = this.previousRightVisible;
            return;
        }

        private function onNextEffectEnd(event:PageEffectEvent) : void
        {
            this.updatePending = false;
            event.target.removeEventListener(PageEffectEvent.EFFECT_END, this.onNextEffectEnd);
            if (this.needUpdate)
            {
                this.addPageContent();
            }
            else
            {
                if (this.leftPageContent.vScrollPosition == 0 && this.previousLeftScrollPosition != 0)
                {
                    return;
                }
                if (this.scrollPercent >= 100)
                {
                    return;
                }
                this.leftPageContent.vScrollPosition = this.leftPageContent.vScrollPosition + this.leftPageContent.vPageScrollSize * 2;
                this.updateRightTextScroll();
            }
            return;
        }

        override public function goPreviousPage() : void
        {
            var _loc_1:Number = NaN;
            if (this.leftPageContent.vScrollPosition < this.leftPageContent.vPageScrollSize)
            {
                _loc_1 = this.leftPageContent.vScrollPosition;
            }
            else
            {
                _loc_1 = this.leftPageContent.vPageScrollSize;
            }
            if (_loc_1 > 0)
            {
                this.leftPageContent.vScrollPosition = this.leftPageContent.vScrollPosition - _loc_1 * 2;
                this.updateRightTextScroll();
            }
            else
            {
                this.hideContent();
            }
            this.doDisplayPreviousPage();
            if (_loc_1 > 0)
            {
                this.leftPageContent.vScrollPosition = this.leftPageContent.vScrollPosition + _loc_1 * 2;
                this.updateRightTextScroll();
            }
            else
            {
                this.showContent();
            }
            return;
        }

        private function doDisplayPreviousPage() : void
        {
            this.updatePending = true;
            PageTurnManager.getInstance().startDoublePageTurn(this, false);
            PageTurnManager.getInstance().addEventListener(PageEffectEvent.EFFECT_END, this.onPrevEffectEnd);
            return;
        }

        private function onPrevEffectEnd(event:PageEffectEvent) : void
        {
            this.updatePending = false;
            event.target.removeEventListener(PageEffectEvent.EFFECT_END, this.onPrevEffectEnd);
            if (this.needUpdate)
            {
                this.addPageContent();
            }
            else
            {
                this.leftPageContent.vScrollPosition = this.leftPageContent.vScrollPosition - this.leftPageContent.vPageScrollSize * 2;
                this.updateRightTextScroll();
            }
            return;
        }

        private function doDisplayNextPage() : void
        {
            this.updatePending = true;
            PageTurnManager.getInstance().startDoublePageTurn(this);
            PageTurnManager.getInstance().addEventListener(PageEffectEvent.EFFECT_END, this.onNextEffectEnd);
            return;
        }

        override public function setTextFormat(param1:TextFormat) : void
        {
            var textFormat:* = param1;
            this.screenFirstCharIndex = this.getCurrentFirstCharIndex();
            this.arrayFunc([this.leftTextField, this.rightTextField], function (param1:TextField) : void
            {
                param1.setTextFormat(textFormat, 0, (param1.text.length - 1));
                param1.defaultTextFormat = textFormat;
                return;
            }
            );
            this.addPageContent();
            this.scrollToCharIndex(this.screenFirstCharIndex);
            return;
        }

        public function restorePosition() : void
        {
            var _loc_1:* = chapterModel.destCharIndex;
            if (_loc_1 == -2)
            {
                this.rightPageContent.vScrollPosition = this.rightPageContent.maxVScrollPosition;
                this.leftPageContent.vScrollPosition = this.rightPageContent.vScrollPosition - this.rightPageContent.vPageScrollSize;
            }
            else if (_loc_1 != -1)
            {
                this.scrollToCharIndex(_loc_1);
            }
            return;
        }

        override public function get scrollPercent() : Number
        {
            var _loc_1:Number = NaN;
            if (!this.leftPageContent.content)
            {
                _loc_1 = 0;
            }
            if (this.rightPageContent.visible && this.rightPageContent.vScrollPosition == this.rightPageContent.maxVScrollPosition)
            {
                _loc_1 = 100;
            }
            else
            {
                _loc_1 = this.leftPageContent.vScrollPosition / this.leftPageContent.maxVScrollPosition * 100;
            }
            if (this.leftPageContent.vScrollPosition == 0 && this.leftPageContent.contentHeight <= this.leftPageContent.vPageScrollSize * 2)
            {
                return NaN;
            }
            return _loc_1;
        }

        private function arrayFunc(param1:Array, param2:Function) : void
        {
            var _loc_3:int = 0;
            while (_loc_3 < param1.length)
            {
                
                param2.apply(this, [param1[_loc_3]]);
                _loc_3++;
            }
            return;
        }

        private function get updatePending() : Boolean
        {
            return this._updatePending;
        }

        private function set updatePending(param1:Boolean) : void
        {
            this._updatePending = param1;
            return;
        }

        private function get needUpdate() : Boolean
        {
            return this._needUpdate;
        }

        private function set needUpdate(param1:Boolean) : void
        {
            this._needUpdate = param1;
            return;
        }

    }
}
